<?php
declare (strict_types = 1);

namespace app\common\validate;

use app\common\config\Client;
use think\Validate;

class User extends ValidateBase
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id' => ['require'],
        'uuid' => ['require'],
        'username' => ['require','checkUnique'=>['table'=>'user','where'=>['delete_time'=>0]]],
        'nickname' => ['require'],
        'password' => ['require','regex'=>'/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])/','length'=>'6,16','confirm'],
        'creator' => ['require'],
        'updater' => ['require']
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'id.require' => 'id不能为空',
        'uuid.require' => 'uuid不能为空',
        'username.require' => '用户名不能为空',
        'username.checkUnique' => '用户已存在',
        'nickname.require' => '昵称不能为空',
        'password.require' => '密码不能为空',
        'password.regex' => '密码必须包含大写字母、小写字母、数字',
        'password.length' => '密码长度为6到16位',
        'creator.require' => '创建者信息不能为空',
        'updater.require' => '更新者信息不能为空',
        'password.confirm' => '两次密码不一致'
    ];

    /**
     * 验证场景
     * 格式：'场景'=>['需要验证的字段']
     */
    protected $scene = [
        'add'  =>  ['uuid','creator','updater','password','username'],
        'del'  =>  ['id','uuid'],
        'query'  =>  ['id','uuid'],
//        'edit'  =>  ['username'=>['unique'],'id','uuid'], 这样写达不到目的，参考下面的sceneEdit方法
        'edit-password'  =>  ['id','uuid','password']
    ];

    /**
     * edit 验证场景定义
     */
    public function sceneEdit()
    {
        return $this->only(['id','uuid','username'])
            ->remove('username', 'require');
    }
}
